{% extends "scheduling/base.html" %}
{% load static %}
{% load tz %}

{% block content %}
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fas fa-calendar-alt me-2"></i>排班管理</h2>
        <a href="{% url 'add_schedule' %}" class="btn btn-primary">
            <i class="fas fa-plus-circle me-2"></i>添加班次
        </a>
    </div>

    <!-- 显示消息 -->
    {% if messages %}
    <div class="messages">
        {% for message in messages %}
        <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
            {{ message }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endfor %}
    </div>
    {% endif %}

    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
    <h4 class="mb-0">所有排班记录</h4>
    <form method="get" class="d-flex">
        <input type="date" name="date" class="form-control me-2" style="width: 200px"
               value="{{ selected_date }}" required>
        <button type="submit" class="btn btn-outline-secondary">
            <i class="fas fa-filter"></i> 筛选
        </button>
    </form>
</div>

        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>发车时间</th>
                            <th>车牌号</th>
                            <th>车型</th>
                            <th>司机</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for schedule in schedules %}
                        <tr>
                            <td>{{ schedule.schedule.departure_time|date:"Y-m-d H:i" }}</td>
                            <td>{{ schedule.schedule.bus.bus_id }}</td>
                            <td>{{ schedule.schedule.bus.model.name }}</td>
                            <td>{{ schedule.schedule.driver.name }}</td>
                            <td>
                                <span class="badge {% if schedule.is_departed %}bg-success{% else %}bg-info{% endif %}">
    {{ schedule.status_label }}
</span>

                            </td>
                           <td>
    <a href="#" class="btn btn-sm btn-outline-primary">
        <i class="fas fa-edit"></i> 编辑
    </a>

    {% if schedule.can_delete %}
        <a href="{% url 'delete_schedule' schedule.schedule.schedule_id %}"
           class="btn btn-sm btn-outline-danger"
           onclick="return confirm('确定要删除这个班次吗？');">
            <i class="fas fa-trash-alt"></i> 删除
        </a>
    {% else %}
        <button class="btn btn-sm btn-outline-danger" disabled>
            <i class="fas fa-trash-alt"></i> 删除
        </button>
        {% if schedule.is_departed %}
            <small class="text-muted">已发车，不可删除</small>
        {% else %}
            <small class="text-muted">临近发车，禁止删除</small>
        {% endif %}
    {% endif %}
</td>

                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="6" class="text-center py-4">暂无排班数据</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}

